package com.delong.service.market;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.market.DataSourceMapper;
import com.delong.model.market.DataSource;

public class DataSourceService {

	public void addElement(DataSource element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeElement(DataSource element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			mapper.changeElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void deleteElements(int id) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			mapper.deleteElement(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public List<DataSource> getListElement(int sourceId, int pageno, int limit, String title, String packagename) {
		SqlSession session = null;
		List<DataSource> result = new ArrayList<DataSource>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			result = mapper.selectList(sourceId, pageno, limit, title, packagename);
			System.out.println("*********");
			System.out.println(result.size());
			System.out.println("*********");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public int getListElementCount(int sourceId, String title, String packagename) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			result = mapper.selectListcount(sourceId, title, packagename);
			System.out.println(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public DataSource getElementById(int id) {
		SqlSession session = null;
		DataSource result = new DataSource();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			DataSourceMapper mapper = session.getMapper(DataSourceMapper.class);
			result = mapper.selectElementById(id);
			System.out.println(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}
}
